package com.mf.all.controller.auth;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.mf.all.entity.dto.DtoUser;
import com.mf.all.entity.req.user.ReqLogin;
import com.mf.all.result.Result;
import com.mf.all.service.ServiceUser;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制器
 */
@RestController
@SaCheckLogin
@RequestMapping("/auth" )
public class ControllerAuth {

    @Resource
    private ServiceUser serviceUser;

    @SaIgnore
    @Operation(summary = "登录" )
    @PostMapping("/login" )
    public Result<DtoUser> login(@Validated @RequestBody ReqLogin reqLogin) {
        return Result.success(serviceUser.doCheck(reqLogin));
    }

    @Operation(summary = "判断是否登录" )
    @RequestMapping("isLogin" )
    public Result<Boolean> isLogin() {
        return Result.success(StpUtil.isLogin());
    }

    @SaIgnore
    @Operation(summary = "用户登出" )
    @PostMapping("/logout" )
    public void logout() {
        if (StpUtil.isLogin()) {
            StpUtil.getSession().logout();
            StpUtil.logout();
        }
    }

}
